package com.xhwl.data.controller;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.common.dto.cent.sdata.SpaceFunctionTypeDTO;
import com.xhwl.common.enums.config.IndustryEnum;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.config.OrganizationConfig;
import com.xhwl.common.query.OrganizationConfigQuery;
import com.xhwl.common.query.cent.sdata.SpaceFunctionTypeQuery;
import com.xhwl.data.service.IOrganizationConfigFeignService;
import com.xhwl.data.service.ISpaceFunctionTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 空间功能类型
 *
 * @Author yf
 * @Description:
 * @Date Create in 9:40 2022/06/07
 */
@RestController
public class SpaceFunctionTypeController {
    @Autowired
    private ISpaceFunctionTypeService spaceFunctionTypeService;

    @Autowired
    private IOrganizationConfigFeignService organizationConfigFeignService;

    /**
     * 新增/修改
     *
     * @param spaceFunctionTypeDTO
     * @return
     */
    @PostMapping("/space-function-type/create")
    public ResultJson create(@RequestBody SpaceFunctionTypeDTO spaceFunctionTypeDTO) {
        if (null == spaceFunctionTypeDTO.getParentId() || null == spaceFunctionTypeDTO.getEnterpriseId()
                || null == spaceFunctionTypeDTO.getName() || null == spaceFunctionTypeDTO.getOrgType()) {
            return ResultJson.fail("参数错误");
        }
        if (null == spaceFunctionTypeDTO.getId()) {
            return spaceFunctionTypeService.create(spaceFunctionTypeDTO);
        }
        return spaceFunctionTypeService.update(spaceFunctionTypeDTO);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @GetMapping("/space-function-type/delete")
    public ResultJson delete(@RequestParam("id") Integer id) {
        return spaceFunctionTypeService.delete(id);
    }

    /**
     * 分页
     *
     * @param spaceFunctionTypeQuery
     * @return
     */
    @PostMapping("/space-function-type/page")
    public ResultJson page(@RequestBody SpaceFunctionTypeQuery spaceFunctionTypeQuery, @RequestHeader Short industryId) {
        if (null == spaceFunctionTypeQuery.getEnterpriseId()) {
            return ResultJson.fail("参数错误");
        }
        if (null == spaceFunctionTypeQuery.getOrgType()) {
            return ResultJson.fail("参数错误");
        }
        spaceFunctionTypeQuery.setIndustryId(industryId);
        return ResultJson.success(spaceFunctionTypeService.page(spaceFunctionTypeQuery));
    }

    /**
     * 列表
     *
     * @param spaceFunctionTypeQuery
     * @return
     */
    @PostMapping("/space-function-type/list")
    public ResultJson list(@RequestBody SpaceFunctionTypeQuery spaceFunctionTypeQuery, @RequestHeader Short industryId) {
        spaceFunctionTypeQuery.setIndustryId(industryId);
        return ResultJson.success(spaceFunctionTypeService.list(spaceFunctionTypeQuery));
    }

    /**
     * 支持配置组织类型列表
     *
     * @return
     */
    @GetMapping("/space-function-type/orgType")
    public ResultJson orgType(@RequestHeader Short industryId,
                              @RequestParam("enterpriseId") Integer enterpriseId) {
        OrganizationConfigQuery query = new OrganizationConfigQuery();
        query.setEnterpriseId(enterpriseId);
        query.setIndustryId(industryId.intValue());
        List<OrganizationConfig> organizationConfigs = organizationConfigFeignService.list(query);
        if (0 == organizationConfigs.size()) {
            query.setEnterpriseId(null);
            organizationConfigs = organizationConfigFeignService.list(query);
        }
        List<JSONObject> orgTypes = organizationConfigs.stream()
                .filter(x -> 1 == x.getLevel() && 2 != x.getType())
                .map(x -> {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("id", x.getType());
                    jsonObject.put("name", x.getName());
                    return jsonObject;
                }).distinct().collect(Collectors.toList());
        return ResultJson.success(orgTypes);
    }
}
